package com.ruixun.shopmanagement.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruixun.shopmanagement.entity.CartItem;
import com.ruixun.shopmanagement.entity.Product;
import com.ruixun.shopmanagement.service.inter.ProductService;

@Controller
@RequestMapping("/cart")
@ResponseBody
public class CartController {

	@Autowired
	ProductService productService;

	@RequestMapping("/addToCart")
	public Map<String, Object> add(HttpServletRequest req, HttpServletResponse res) {
		Map<String, Object> respMap = new HashMap<>();
		String pid = req.getParameter("pid");
		// 1.判断是否有购物车
		Map<String, CartItem> cart = (Map<String, CartItem>) req.getSession().getAttribute("cart");
		int countNum = Integer.parseInt(req.getParameter("count"));
		if (cart != null) {
			// 1.1 有-->判断购物车中是否有该商品
			if (cart.containsKey(pid)) {
				// 1.1.1 有-->购物车中当前商品数量+countNum
				CartItem cartItem = cart.get(pid);
				cartItem.setCount(cartItem.getCount() + countNum);
			} else {
				// 1.1.2 没有-->购物车中加入该购物项
				CartItem cartItem = new CartItem();
				Product product = productService.getProductById(pid);
				// 通过beanutils copy 商品基本属性
				try {
					BeanUtils.copyProperties(cartItem, product);
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
				// 设置其他属性
				cartItem.setCount(countNum);
				cart.put(pid, cartItem);
			}
		} else {
			// 1.2 没有 -->
			// 创建一个购物车 map<String ,CartItem>
			cart = new HashMap<>();
			// 购物车中加入该购物项
			// 1.1.2 没有-->购物车中加入该购物项
			CartItem cartItem = new CartItem();
			Product product = productService.getProductById(pid);
			// 通过beanutils copy 商品基本属性
			try {
				BeanUtils.copyProperties(cartItem, product);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
			// 设置其他属性
			cartItem.setCount(countNum);
			cart.put(pid, cartItem);
			// 将购物车存入session
			req.getSession().setAttribute("cart", cart);
		}
		respMap.put("cart", cart);
		respMap.put("state", true);
		return respMap;
	}

	@RequestMapping("/updateCount")
	public Map<String, Object> updateCount(HttpServletRequest req, HttpServletResponse res) {
		Map<String, Object> respMap = new HashMap<>();
		// 获取参数
		String count = req.getParameter("count");
		String pid = req.getParameter("pid");
		// 获取购物车
		Map<String, CartItem> cart = (Map<String, CartItem>) req.getSession().getAttribute("cart");
		// 获取购物项
		CartItem cartItem = cart.get(pid);
		// 更新数量
		cartItem.setCount(Integer.valueOf(count));
		// 计算小计
		Double subTotal = cartItem.getSubTotal();
		// 循环遍历计算总金额
		Double totalPrice = 0d;
		Set<Entry<String, CartItem>> entrySet = cart.entrySet();
		for (Entry<String, CartItem> entry : entrySet) {
			CartItem ci = entry.getValue();
			totalPrice += ci.getSubTotal();
		}
		// 传递字符串 小计&&&总金额
		respMap.put("subTotal", subTotal);
		respMap.put("totalPrice", totalPrice);
		respMap.put("state", true);
		return respMap;
	}

	@RequestMapping("clearCart")

	public boolean clear(HttpServletRequest req, HttpServletResponse res) {
		//将session中的cart移除
		req.getSession().removeAttribute("cart");
		return true;
	}
	@RequestMapping("getCart")

	public Map<String, CartItem> getCart(HttpServletRequest req, HttpServletResponse res) {
		//将session中的cart移除
		Map<String, CartItem> cart = (Map<String, CartItem>) req.getSession().getAttribute("cart");
		return cart;
	}
	
	@RequestMapping("deleteCartItem")
	public boolean deleteCartItem(HttpServletRequest req, HttpServletResponse res) {
		String pid = req.getParameter("pid");
		//获取购物车
		Map<String,CartItem> cart = (Map<String, CartItem>) req.getSession().getAttribute("cart");
		//从购物车中干掉该购物项
		cart.remove(pid);
		return true;
	}
}
